package datasource;

import datasource.config.C3P0Config;
import jdbctest.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/14.
 */
public class UserDao {
    public int pageSize=3;
    public List<User> findPage(int page) throws SQLException {
        //获得数据库连接
        Connection conn= C3P0Config.getConnection();
        List<User> list =new ArrayList<>();
        //编写SQL语句
        String sql="SELECT * FROM tablename1  ORDER BY id DESC LIMIT ?,?";
        //预编译处理
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setInt(1,(page-1)*pageSize);
        ps.setInt(2,pageSize);
        //执行语句
        ResultSet result=ps.executeQuery();
        //处理结果
        while(result.next()){
            User user=new User();
            user.setId((result.getInt(1)));
            user.setName(result.getString(2));
            user.setSb(result.getString(3));
            list.add(user);
        }
        return list;
    }
}
